<?php
set_time_limit(0);
if (isset($_POST['uid']) && isset($_POST['n'])){
    include '../c/config.php';
    $uid=addslashes(trim($_POST['uid']));
    $name=addslashes(trim($_POST['n']));
    $phone=addslashes(trim($_POST['ph']));
    $nianji=addslashes(trim($_POST['nianji']));
    
    $mysqli = new mysqli($dbhost, $dbuser, $dbpwd, $dbName);
    if (!$mysqli->set_charset("utf8")) {printf("Error loading character set utf8: %s\n", $mysqli->error);}
    $yanChi=array(
       1 => 2448, 
       2 => 3779,
       3 => 5833,
       4 => 7241,
       5 => 6352,
       6 => 4473,
       7 => 3544,
       8 => 2635,
       9 => 1726,
       10 => 3817,
       11 => 4128,
       12 => 5239,
       13 => 6361,
       14 => 7479,
       15 => 3884,
       16 => 5995,
       17 => 6717,
       18 => 7829,
       19 => 9938,
       20 => 4962,
      ); 

    $mysqli->query("INSERT INTO `u_log` (`id`, `uid`,`name`,`nianji`,`phone`,`createtime`) VALUES (NULL,'".$uid."','".$name."','".$nianji."','".$phone."','".date("Y-m-d H:i:s")."')");
    $table='';

   $code=addslashes(trim($_POST['code']));
   $code2=addslashes(trim($_POST['code2']));
    if (isset($yanChi[$code2])) {
        if($yanChi[$code2] != $code){
           $falg=true;
           if(6 == $nianji && 2113 == $code){
                $falg=false;
           }
           if(7 == $nianji && 2113 == $code){
                $falg=false;
           }
           if(8 == $nianji && 2113 == $code){
                $falg=false;
           }
           if(9 == $nianji && 2113 == $code){
                $falg=false;
           }
           if(true == $falg){
              echo json_encode(array('s'=>'n','e'=>'code'));
              exit;
           }
        }
    }else{

    exit;
    }

   if (6 == $nianji) {
     $table='s6';
     $phoneFile='6';
   }
   if (7 == $nianji) {
     $table='s7';
     $phoneFile='7';
   }
   if (8 == $nianji) {
     $table='s8';
     $phoneFile='8';
   }
   if (9 == $nianji) {
     $table='s9';
     $phoneFile='9';
   }
   $phoneList=json_decode(file_get_contents($phoneFile.'.txt'), true);


  if (empty($table)) {
      exit;
  }
   
  if (in_array($phone, $phoneList)){
       $queryJ=$mysqli->query("SELECT id,qu,school,name from `".$table."` where phone='".$phone."' and name='".$name."' limit 0,1 ");
  }else{
       $queryJ=$mysqli->query("SELECT id,qu,school,name from `".$table."` where phone='".$phone."' limit 0,1 ");
  }
    
  if($queryJ->num_rows>0){
       $getU=$queryJ->fetch_assoc();
       if (isset($getU['id']) && !empty($getU['id']) ) {
          $mysqli->query("INSERT INTO `u` (`id`, `uid`,`schoolid`,`createtime`) VALUES (NULL,'".$uid."','".$getU['id']."','".date("Y-m-d H:i:s")."')");
           echo json_encode(array('s'=>'y','q'=>$getU['qu'],'x'=>$getU['school'],'zd'=>$getU['id'],'n'=>$getU['name']));
       }else{
        echo json_encode(array('s'=>'n','e'=>'u'));
        exit;
       }
    
      
    }else{
    
       echo json_encode(array('s'=>'n','e'=>'u'));
    }
    $mysqli->close();
}else{
    echo json_encode(array('s'=>'n','e'=>'u'));
}

?>